package servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.yunhe.dao.ReplyDao;
import cn.yunhe.dao.impl.ReplyDaoImpl;
import cn.yunhe.entity.Reply;
import cn.yunhe.entity.Topic;
import cn.yunhe.entity.User;
import cn.yunhe.util.DateUtil;

/**
 * Servlet implementation class addReply
 */
@WebServlet("/addReply")
public class addReply extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addReply() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			ReplyDao replyDao=new ReplyDaoImpl();
			String content=request.getParameter("replyContent");//获取回复内容
			String tidStr= request.getParameter("tid");//获取回帖所属的主贴ID
			//String ParentId=request.getParameter("ParentId");
			//int pid=Integer.parseInt(ParentId);
			int tid=Integer.parseInt(tidStr);
			User user=(User)request.getSession().getAttribute("CURR_USER");
			Reply reply = new  Reply(content, DateUtil.dateTimeToString(new Date()), user, new Topic(tid));
			boolean result=replyDao.addReply(reply);
			String str=null;
			if(result){
				str="回帖成功";
				System.out.println("===============");
			}else{
				str="回帖失败";
				System.out.println("-----------------------");
			}
			request.getRequestDispatcher("getTopicDetail?tid="+tid+"&msg="+str).forward(request, response);
			
			
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
